﻿using FibMatrix.Task;

namespace FibMatrix
{
	[FM_Mono.IgnoreGen]
	public class LoadAssetFromBundlePipe : IResourceLoadPipe
	{
		public FMTask Handle(IResourcePipelineContext rawContext, out ResourceError error)
		{
			error = ResourceError.None;
			ResourcePipelineContext context = rawContext as ResourcePipelineContext;
			AssetBundleLoadItem mainHint = context.item as AssetBundleLoadItem;

			if (mainHint.assetHandle == null)
			{
				if (mainHint.bundleHandle != null)
				{
					mainHint.assetHandle = mainHint.bundleHandle.LoadAsset(mainHint.itemID, context.type);
					if (mainHint.assetHandle != null)
					{
						++mainHint.objRefCount;
					}
					else
					{
						error = ResourceError.AssetLoadError;
						return null;
					}
				}
				else
				{
					error = ResourceError.HandleLostWhenLoadAsset;
					return null;
				}
			}
			return null;
		}
	}
}
